﻿#region Nguyễn Trường Sơn
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Collections;

namespace GUI.KhachHangTraLaiBanBuon
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            if (capnhat == true)
            {
                if (GUI.KhachHangTraLaiBanBuon.frmXuLy.CheckClose == true)
                {
                    Them();
                }
                GUI.KhachHangTraLaiBanBuon.frmXuLy.CheckClose = false;
                HienThi();
            }
        }
        #endregion

        #region New Object
        Boolean CheckClose = false;
        private string MaKho = string.Empty;
        private string TenKho = string.Empty;
        private Entities.KhachHangTraLaiBanBuon[] DanhSach;
        private Entities.KhachHangTraLaiBanBuon BanGhi;
        private string MaKhachHangTraLaiBanBuon = string.Empty;

        public frmQuanLy()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            try
            {
                LayCaiDatKho();
                if (MaKho == null)
                {
                    MessageBox.Show("Chưa cài đặt kho hàng");
                }
                else
                {
                    HienThi();
                }
                this.WindowState = FormWindowState.Maximized;
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Cai Dat Kho
        private void LayCaiDatKho()
        {
            try
            {
                SetupConfig.Variable vl = new SetupConfig.Variable();
                vl.HanhDong = "1";
                //vl.HanhDong = "2";
                System.Reflection.Assembly getfile = System.Reflection.Assembly.GetExecutingAssembly();
                vl.FileName = getfile.Location + ".config";
                //vl.KeyName = "ChatService";
                SetupConfig.Variable[] list = SetupConfig.Config.GetConfig(vl);
                this.MaKho = list[0].KeyValue;
                this.TenKho = list[1].KeyValue;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Hiển Thị
        private void HienThi()
        {
            try
            {
                Entities.KhachHangTraLaiBanBuon row = new Entities.KhachHangTraLaiBanBuon();
                row.HanhDong = "1";
                row.MaKhachHangTraLaiBanBuon = "";
                DanhSach = Common.StaticVariable.InterfaceObject.SelectKhachHangTraLaiBanBuon(row);
                ArrayList list = new ArrayList();
                for (int i = 0; i < DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaKho.ToUpper() == this.MaKho.ToUpper())
                    { list.Add(DanhSach[i]); }
                }
                int k = list.Count;
                if (k == 0)
                {
                    DanhSach = null;
                }
                else
                {
                    DanhSach = new Entities.KhachHangTraLaiBanBuon[k];
                    for (int i = 0; i < k; i++)
                    {
                        DanhSach[i] = (Entities.KhachHangTraLaiBanBuon)list[i];
                    }
                    dgvHienThi.DataSource = DanhSach;
                }
                HienThiListview();
            }
            catch (Exception)
            { }
        }

        private void HienThiListview()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.KhachHangTraLaiBanBuon[] nv = new Entities.KhachHangTraLaiBanBuon[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["HanhDong"].HeaderText = "STT";
                dgvHienThi.Columns["HanhDong"].Visible = true;
                dgvHienThi.Columns["MaKhachHangTraLaiBanBuon"].HeaderText = "Mã hóa đơn trả lại";
                dgvHienThi.Columns["MaKhachHangTraLaiBanBuon"].Visible = true;
                dgvHienThi.Columns["NgayNhap"].HeaderText = "Ngày trả";
                dgvHienThi.Columns["NgayNhap"].Visible = true;
                dgvHienThi.Columns["TenKho"].HeaderText = "Tên kho";
                dgvHienThi.Columns["TenKho"].Visible = true;
                dgvHienThi.Columns["TienBoiThuong"].HeaderText = "Tổng tiền";
                dgvHienThi.Columns["TienBoiThuong"].Visible = true;
                dgvHienThi.Columns["GhiChu"].HeaderText = "Ghi chú";
                dgvHienThi.Columns["GhiChu"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        //lay ban ghi de sua hoac xem
        private void GetRow()
        {
            try
            {
                if (DanhSach == null) { return; }
                for (int i = 0; i < this.DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaKhachHangTraLaiBanBuon == this.MaKhachHangTraLaiBanBuon)
                    {
                        BanGhi = DanhSach[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Thêm
        private void Them()
        {
            try
            {
                GUI.KhachHangTraLaiBanBuon.frmXuLy frm = new GUI.KhachHangTraLaiBanBuon.frmXuLy("Them", this.BanGhi);
                frm.GetData = new GUI.KhachHangTraLaiBanBuon.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Xem
        private void Xem()
        {
            try
            {
                if (this.BanGhi == null)
                {
                    MessageBox.Show("Bạn phải chọn hóa đơn.", "Thông báo");
                    return;
                }
                GUI.KhachHangTraLaiBanBuon.frmXuLy frm = new GUI.KhachHangTraLaiBanBuon.frmXuLy("Xem", this.BanGhi);
                frm.GetData = new GUI.KhachHangTraLaiBanBuon.frmXuLy.GetString(GetValue);
                //frm.MdiParent = this.MdiParent;
                frm.Show();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Event
        private void btnThem_Click(object sender, EventArgs e)
        {
            this.Them();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            this.Xem();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvHienThi.Rows.Count <= 0)
                { return; }
                this.MaKhachHangTraLaiBanBuon = dgvHienThi.CurrentRow.Cells["MaKhachHangTraLaiBanBuon"].Value.ToString();
                this.GetRow();
            }
            catch (Exception)
            {
            }
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Xem();
        }
        #endregion
    }
}
#endregion
